package yelmor.comm100.controller;

import org.aopalliance.reflect.Code;
import org.igniterealtime.restclient.entity.MUCRoomEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import yelmor.comm100.bean.OpenfireApi;
import yelmor.comm100.domain.CoUser;
import yelmor.comm100.domain.CoUserRole;
import yelmor.comm100.repository.CoUserRepository;
import yelmor.comm100.repository.CoUserRoleRepository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Map;
import yelmor.comm100.util.CodeGrenrator;
import yelmor.comm100.util.SecurityUtil;

/**
 * Created by yel on 4/28/16.
 */
@Controller
public class Register {


  @Autowired
  private CoUserRepository coUserRepository;

  @Autowired
  private CoUserRoleRepository coUserRoleRepository;

  @Autowired
  private PasswordEncoder passwordEncoder;

  @Autowired
  private OpenfireApi openfireApi;

  @RequestMapping(path="/register", method = RequestMethod.GET)
  public String registerGet(Model model) {
    if(SecurityUtil.getCurrentUserName() != "anonymousUser") {
      return "/";
    }
    Map<String, Object> modelMap = model.asMap();
    for (String s : modelMap.keySet()) {
      System.out.println("..." + modelMap.get(s).toString());
    }
    return "register";
  }

  @RequestMapping(path="/register", method = RequestMethod.POST)
  public String registerPost(Model model, HttpServletRequest request, HttpServletResponse response) {
    String userName = request.getParameter("username");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    System.out.println("register with " + userName + ", " + password + ".");
    if(coUserRepository.findOne(userName) != null) {
      model.addAttribute("register_fail", true);
      model.addAttribute("fail_reason", "用户名已被注册");
      return "register";
    } else {
      CoUser coUser = new CoUser();
      coUser.setUserName(userName);
      coUser.setUserMail(email);
      coUser.setEnPassword(passwordEncoder.encode(password));
      coUser.setIsAdmin(false);
      coUser.setIsEnabled(true);
      coUser.setRegisterDate(new Date());
      coUser.setSiteCode(CodeGrenrator.generateSiteCode());
      CoUserRole coUserRole = new CoUserRole();
      coUserRole.setUserName(coUser.getUserName());
      coUserRole.setRole("ROLE_USER");
      coUserRepository.save(coUser);
      coUserRoleRepository.save(coUserRole);
      setNewSite(coUser.getSiteCode());
      return "redirect:/login";
    }
  }

  private void setNewSite(String siteCode) {
    openfireApi.createOfGroup(siteCode);
    MUCRoomEntity serviceChatRoom = new MUCRoomEntity("service-" + siteCode, siteCode, "services chating room of " + siteCode);
    openfireApi.getRestApiClient().createChatRoom(serviceChatRoom);

/*    String name = CodeGrenrator.generateSiteCode();
    String userName = name + "-service-" + siteCode;
    openfireApi.createOfUser(userName, name, "fortest");
    openfireApi.addUserToGroup(userName, siteCode);*/
  }
}
